package com.sui.bigdata.rtcadmin.product;

import com.feidee.fdcommon.model.StringResponse;
import com.feidee.fdcommon.util.HttpUtil;
import org.apache.http.Consts;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

/**
 * @author YongChen
 * @date 2019/11/25 10:08
 * @description
 * @email yong_chen@sui.com
 */
public class submit {
    public static void main(String[] args) throws Exception {


        String url =  "http://adam.feidee.com/rtc-admin/job?action=submit";
//        String url = "http://10.201.7.132:18001/rtc-admin/job?action=submit";
//        String url = "http://localhost:18001/rtc-admin/job?action=submit";

//        String sql = " select user_id from  device_info_1 where NotBlank(user_id) and user_id='%草泥马';" ;


        String sql =
                "\n" +
                        "INSERT INTO ResultTable_applist   \n" +
                        "SELECT kn_user.suid as suid,\n" +
                        "         md5(kn_user.suid) AS suidmd5,\n" +
                        "         apptags.client_uid AS user_id,\n" +
                        "         apptags.total_app_cnt as total_app_cnt,\n" +
                        "         apptags.is_dai_cnt as is_dai_cnt,\n" +
                        "         apptags.is_work_study_cnt as is_work_study_cnt,\n" +
                        "         apptags.is_read_cnt as is_read_cnt,\n" +
                        "         apptags.is_health_cnt as is_health_cnt,\n" +
                        "         apptags.is_video_cnt as is_video_cnt,\n" +
                        "         apptags.is_life_cnt as is_life_cnt,\n" +
                        "         apptags.is_other_cnt as is_other_cnt,\n" +
                        "         apptags.low_fraud_app as low_fraud_app,\n" +
                        "         apptags.fraud_score  as fraud_score,\n" +
                        "         LOCALTIMESTAMP AS apptags_update_time\n" +
                        "FROM(SELECT client_uid,\n" +
                        "         md5(client_uid) AS userid_md5,\n" +
                        "         count(distinct packName) AS total_app_cnt,\n" +
                        "         count(distinct CASE\n" +
                        "                    WHEN name LIKE '%贷%' THEN\n" +
                        "                    name\n" +
                        "                    WHEN name LIKE '%金融%' THEN\n" +
                        "                    name\n" +
                        "                    WHEN name LIKE '%钱%' THEN\n" +
                        "                    name\n" +
                        "                    WHEN name LIKE '%借%' THEN\n" +
                        "                    name\n" +
                        "                    WHEN name LIKE '%现金%' THEN\n" +
                        "                    name\n" +
                        "                    WHEN name LIKE '%分期%' THEN\n" +
                        "                    name\n" +
                        "                    ELSE null\n" +
                        "                    END) AS is_dai_cnt,\n" +
                        "         count(distinct case\n" +
                        "        WHEN app_category = '办公学习类' THEN\n" +
                        "        packName\n" +
                        "        ELSE null end) AS is_work_study_cnt, count(distinct case\n" +
                        "        WHEN app_category = '阅读类' THEN\n" +
                        "        packName\n" +
                        "        ELSE null end) AS is_read_cnt, count(distinct case\n" +
                        "        WHEN app_category = '医疗健康类' THEN\n" +
                        "        packName\n" +
                        "        ELSE null end) AS is_health_cnt, count(distinct case\n" +
                        "        WHEN app_category = '影音娱乐类' THEN\n" +
                        "        packName\n" +
                        "        ELSE null end) AS is_video_cnt, count(distinct case\n" +
                        "        WHEN app_category = '生活类' THEN\n" +
                        "        packName\n" +
                        "        ELSE null end) AS is_life_cnt, count(distinct case\n" +
                        "        WHEN app_category = '其他类' THEN\n" +
                        "        packName\n" +
                        "        ELSE null end) AS is_other_cnt, count(distinct case\n" +
                        "        WHEN app_fraud_score<quantile THEN\n" +
                        "        packName\n" +
                        "        ELSE null end) AS low_fraud_app,\n" +
                        "        case when max(low10_cnt)=0 then -2 WHEN  max(app_fraud_score) is null \n" +
                        "        THEN -2 else max(app_fraud_score) end as fraud_score\n" +
                        "    FROM \n" +
                        "        (SELECT appinfos.client_uid AS client_uid,\n" +
                        "         appinfos.proctime AS proctime,\n" +
                        "         appinfos.packName AS packName,\n" +
                        "         appinfos.name AS name,\n" +
                        "         appinfos.app_category AS app_category,\n" +
                        "         fraud_score.app_fraud_score AS app_fraud_score,\n" +
                        "         fraud_score.quantile AS quantile,\n" +
                        "         fraud_score.low10_cnt AS low10_cnt\n" +
                        "        FROM \n" +
                        "            (SELECT a1.client_uid AS client_uid,\n" +
                        "         a1.proctime AS proctime,\n" +
                        "         a1.packName AS packName,\n" +
                        "         a1.name AS name,\n" +
                        "         (case when a2.app_category is not null then a2.app_category else '其他类' end )AS app_category\n" +
                        "            FROM \n" +
                        "                (SELECT client_uid,\n" +
                        "                proctime,\n" +
                        "                packName,\n" +
                        "                name\n" +
                        "                FROM \n" +
                        "                    (SELECT app.client_uid AS client_uid ,\n" +
                        "         app.proctime,\n" +
                        "         LOWER(T.packName) AS packName,T.name as name\n" +
                        "                    FROM applist_info AS app, lateral table(SplitJsonUdtf(app_info)) AS T(name,packName)) apptable)a1\n" +
                        "                    LEFT JOIN dm_af_lxh_app_package_category a2 \n" +
                        "                        ON a1.packName=a2.package_name) appinfos\n" +
                        "                    LEFT JOIN dm_af_lxh_app_package_fraud_score_partition fraud_score\n" +
                        "                        ON appinfos.packName=fraud_score.package_name) t1\n" +
                        "                    GROUP BY  SESSION ( proctime, INTERVAL '1' SECOND), client_uid)apptags\n" +
                        "                LEFT JOIN kn_user_suid kn_user on apptags.userid_md5=kn_user.user_id_md5 ;\n" +
                        "\n" +
                        "INSERT INTO ResultTable_geohash\n" +
                        "SELECT \n" +
                        "    md5(geohash) AS geohashmd5,\n" +
                        "    geohash AS geohash ,\n" +
                        "    count(distinct CASE WHEN active_status IN (3,4,10) THEN suid ELSE NULL END) AS geohash_credit_suid_cnt,\n" +
                        "        count(distinct CASE WHEN active_status = 4 THEN suid ELSE NULL END) AS geohash_credit_fail_suid_cnt,\n" +
                        "        LOCALTIMESTAMP AS geohash_update_time\n" +
                        "FROM \n" +
                        "    (SELECT geohash AS geohash,\n" +
                        "         suid AS suid,\n" +
                        "         proctime AS proctime,\n" +
                        "         active_status AS active_status,\n" +
                        "         apply_id AS apply_id,\n" +
                        "         customer_id AS customer_id,\n" +
                        "         business_type AS business_type\n" +
                        "        FROM \n" +
                        "            (SELECT loc.geohash AS geohash,\n" +
                        "         loc.suid AS suid,\n" +
                        "         active.active_status AS active_status,\n" +
                        "         active.apply_id AS apply_id,\n" +
                        "         active.customer_id AS customer_id,\n" +
                        "         loc.proctime\n" +
                        "            FROM \n" +
                        "                (SELECT location.geohash AS geohash,\n" +
                        "         locationall.suid AS suid,\n" +
                        "         location.proctime\n" +
                        "                FROM \n" +
                        "                    (SELECT GeoHashUDF(longitude,\n" +
                        "        latitude,\n" +
                        "        6) AS geohash,\n" +
                        "         proctime\n" +
                        "                    FROM device_location\n" +
                        "                    WHERE ABS(longitude)>1\n" +
                        "                            AND ABS(latitude)>1 ) location\n" +
                        "                    JOIN adl_bd_device_location_t locationall\n" +
                        "                        ON locationall.geohash=location.geohash)loc\n" +
                        "                    JOIN txmxpfk_bdl_suigo_loan_customer_app_active active\n" +
                        "                        ON loc.suid=active.client_uname\n" +
                        "                    WHERE active.active_status in(3,4,10) ) active_info\n" +
                        "                    JOIN bdl_suigo_business_apply apply\n" +
                        "                        ON active_info.apply_id=apply.id\n" +
                        "                    WHERE apply.business_type=4) allinfo\n" +
                        "                GROUP BY  SESSION ( proctime, INTERVAL '1' SECOND), geohash;\n" +
                        "\n" +
                        "INSERT INTO ResultTable_geohash1\n" +
                        "SELECT \n" +
                        "    md5(geohash) AS geohashmd5,\n" +
                        "    geohash AS geohash,\n" +
                        "        count(distinct CASE WHEN putout_status=4 THEN suid ELSE NULL END) AS geohash_putout_suc_suid_cnt, \n" +
                        "        count(distinct CASE WHEN putout_status=2 THEN suid ELSE NULL END) AS geohash_putout_fail_suid_cnt,\n" +
                        "        LOCALTIMESTAMP AS geohash_update_time\n" +
                        "FROM \n" +
                        "    (SELECT geohash AS geohash,\n" +
                        "         suid AS suid,\n" +
                        "         proctime AS proctime,\n" +
                        "         active_status AS active_status,\n" +
                        "         apply_id AS apply_id,\n" +
                        "         customer_id AS customer_id,\n" +
                        "         business_type AS business_type,\n" +
                        "         putout_status AS putout_status\n" +
                        "    FROM \n" +
                        "        (SELECT geohash AS geohash,\n" +
                        "         suid AS suid,\n" +
                        "         proctime AS proctime,\n" +
                        "         active_status AS active_status,\n" +
                        "         putout.apply_id AS apply_id,\n" +
                        "         putout.customer_id AS customer_id,\n" +
                        "         putout.putout_status AS putout_status\n" +
                        "        FROM \n" +
                        "            (SELECT loc1.geohash AS geohash,\n" +
                        "         loc1.suid AS suid,\n" +
                        "         active1.active_status AS active_status,\n" +
                        "         active1.apply_id AS apply_id,\n" +
                        "         active1.customer_id AS customer_id,\n" +
                        "         loc1.proctime\n" +
                        "            FROM \n" +
                        "                (SELECT location1.geohash AS geohash,\n" +
                        "         locationall1.suid AS suid,\n" +
                        "         location1.proctime\n" +
                        "                FROM \n" +
                        "                    (SELECT GeoHashUDF(longitude,\n" +
                        "        latitude,\n" +
                        "        6) AS geohash,\n" +
                        "         proctime\n" +
                        "                    FROM device_location\n" +
                        "                    WHERE ABS(longitude)>1\n" +
                        "                            AND ABS(latitude)>1 ) location1\n" +
                        "                    JOIN adl_bd_device_location_t locationall1\n" +
                        "                        ON locationall1.geohash=location1.geohash)loc1\n" +
                        "                    JOIN txmxpfk_bdl_suigo_loan_customer_app_active active1\n" +
                        "                        ON loc1.suid=active1.client_uname\n" +
                        "                    WHERE active1.active_status in(3,4,10) ) active_info1\n" +
                        "                    JOIN bdl_suigo_business_putout putout\n" +
                        "                        ON active_info1.customer_id=putout.customer_id) active_infos1\n" +
                        "                     JOIN  bdl_suigo_business_apply apply\n" +
                        "                        ON active_infos1.apply_id=apply.id\n" +
                        "                    WHERE apply.business_type=4) allinfo\n" +
                        "                GROUP BY  SESSION ( proctime, INTERVAL '1' SECOND), geohash;";

        String data = "{\"execSql\":\"" + sql +
                "\",\"jobName\":\"anti_fraud_model_portrait_tags\"," +
                "\"developer\":\"dongguosheng\"," +
                "\"alarmGroupPhone\":\"13554915326,18948337291\"," +
                "\"parallelism\":\"5\"," +
                "\"queue\":\"root.bd_app.fdbd_rtc\"," +
                "\"jobManagerMemory\":\"1024\"," +
                "\"taskManagerMemory\":\"5120\"," +
                "\"slots\":\"1\"," +
                "\"extendParam\":{\"sql.checkpoint.interval\":300000}," +
                "\"savepoint\":\"true\"}";

//        String data = "{\"jobName\":\"risk_control_p1\",'savepoint':'true'}";
        System.out.println(data);
        StringEntity stringEntity = new StringEntity(data, Consts.UTF_8);
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");

        HttpPost httpPost=new HttpPost(url);

        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(500000).build();
        httpPost.setEntity(stringEntity);
        httpPost.setConfig(requestConfig);
        StringResponse response = HttpUtil.post(httpPost);
        System.out.println(">>>>> "+response.getResponseBody());
    }
}

